<template>
  <div class="user-layout-container">
    <div class="user-sidebar">
      <!-- 添加返回首页按钮到顶部 -->
      <div class="back-to-home">
        <router-link to="/" class="home-link">
          <HomeOutlined class="home-icon" />
          <span>返回首页</span>
        </router-link>
      </div>
      
      <div class="user-profile-summary">
        <div class="avatar-container">
          <img
            v-if="userStore.userInfo?.avatar"
            :src="getStaticUrl(userStore.userInfo.avatar)"
            class="user-avatar"
            alt="用户头像"
          />
          <div v-else class="avatar-placeholder">
            {{ getInitials(userStore.userInfo?.nickname || userStore.userInfo?.username) }}
          </div>
        </div>
        <h3 class="username">{{ userStore.userInfo?.nickname || userStore.userInfo?.username }}</h3>
      </div>
      
      <div class="sidebar-menu">
        <router-link
          v-for="menu in userMenus"
          :key="menu.path"
          :to="menu.path"
          class="menu-item"
          :class="{ active: isActive(menu.path) }"
        >
          <component :is="menu.icon" class="menu-icon" />
          <span class="menu-text">{{ menu.name }}</span>
        </router-link>
      </div>
    </div>
    
    <div class="user-content">
      <router-view></router-view>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { UserOutlined, FileTextOutlined, SettingOutlined, HomeOutlined } from '@ant-design/icons-vue';
import { getStaticUrl } from '@/utils/url';
const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

// 用户菜单项 - 使用组件而不是文本图标
const userMenus = [
  { name: '个人资料', path: '/user/profile', icon: UserOutlined },
  { name: '我的文章', path: '/user/articles', icon: FileTextOutlined },
  { name: '账号设置', path: '/user/settings', icon: SettingOutlined }
];

// 检查当前路由是否激活
const isActive = (path) => {
  return route.path === path || route.path.startsWith(path + '/');
};

// 获取用户名首字母用于头像占位符
const getInitials = (name) => {
  if (!name) return '用户';
  return name.charAt(0).toUpperCase();
};
</script>

<style scoped>
.user-layout-container {
  display: flex;
  min-height: calc(100vh - 64px - 100px); /* 减去顶部导航和底部高度 */
  background-color: #f5f5f7;
  margin: 20px auto;
  max-width: 1200px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.user-sidebar {
  width: 240px;
  background-color: white;
  padding: 20px 0;
  border-right: 1px solid #eee;
  display: flex;
  flex-direction: column;
}

.user-content {
  flex: 1;
  padding: 0;
  background-color: white;
  overflow: hidden;
}

.user-profile-summary {
  text-align: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.avatar-container {
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}

.user-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #f0f0f0;
}

.avatar-placeholder {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background-color: #1890ff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  font-weight: bold;
}

.username {
  font-size: 16px;
  margin: 10px 0 0;
  color: #333;
}

.sidebar-menu {
  margin-top: 20px;
  flex: 1;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  color: #5f6368;
  text-decoration: none;
  transition: background-color 0.3s, color 0.3s;
}

.menu-item:hover {
  background-color: #f6f8fa;
  color: #1890ff;
}

.menu-item.active {
  background-color: #e6f7ff;
  color: #1890ff;
  border-right: 3px solid #1890ff;
}

.menu-icon {
  margin-right: 12px;
  font-size: 18px;
}

.menu-text {
  font-size: 14px;
}

/* 返回首页按钮样式 - 调整为顶部显示 */
.back-to-home {
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.home-link {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  color: #42b983;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s;
}

.home-link:hover {
  background-color: #f0f9f4;
  color: #3da776;
}

.home-icon {
  margin-right: 8px;
  font-size: 16px;
}

@media (max-width: 768px) {
  .user-layout-container {
    flex-direction: column;
  }
  
  .user-sidebar {
    width: 100%;
    padding: 10px 0;
  }
  
  .user-profile-summary {
    padding: 10px;
  }
  
  .user-avatar, .avatar-placeholder {
    width: 60px;
    height: 60px;
  }
  
  .sidebar-menu {
    display: flex;
    overflow-x: auto;
    margin-top: 0;
  }
  
  .menu-item {
    flex-direction: column;
    padding: 8px 16px;
    flex-shrink: 0;
  }
  
  .menu-icon {
    margin-right: 0;
    margin-bottom: 4px;
  }
  
  .back-to-home {
    padding: 8px 0;
    border-bottom: 1px solid #eee;
  }
}
</style> 